<?php
class Controller_User extends FLEA_Controller_Action
{
	private $_ModelSysRoles;
	private $_ModelSysUsers;
	private $_ModelUserInfo;
	private $siteInfo;
	private $user;
	private $_ModelNotice;

	function Controller_User(){
		load_language('ui');
		$this->siteInfo = FLEA::getAppInf('siteInfo');

		$rbac =& FLEA::getSingleton('FLEA_Rbac');
		$this->user = $rbac->getUser();//get login user's information
		
		$this->_ModelNotice = & FLEA::getSingleton('Model_Notice');
	}

	function actionIndex(){

		$this ->_ModelUserInfo = & FLEA::getSingleton('Model_UserInfo');
		$userInfo = $this ->_ModelUserInfo->analysisUserInfo($this ->_ModelUserInfo,$this->user['ID']);//get more user's information
		
		$conditions = 'type = 1';
		$conditions2 = 'type = 2';
		$limit = 5;
		
		$list = $this->_ModelNotice->findAll($conditions,'',$limit);
		$list2 = $this->_ModelNotice->findAll($conditions2,'',$limit);

		$viewData = array(
			'siteInfo' => $this -> siteInfo,
			'user' => $this->user,
			'userInfo' => $userInfo,
			'time' => date('Y-m-d H:i:s'),
			'list' => $list,
			'list2' => $list2,
		);
		$this->_executeView('UserIndex.html',$viewData);
	}

	function actionLogin(){

		$imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');

		$imgcodeValue = addslashes(strtolower(trim($_POST['verifycode'])));
		$username = addslashes(trim($_POST['username']));
		$password = addslashes($_POST['passwd']);

		do {
			/*
			 * check verifycode
			 */
			if (!$imgcode->check($imgcodeValue)) {
				$msg = _T('ui_l_invalid_imgcode');
				js_alert($msg,'',url('Default'));
			}
			$imgcode->clear();// clear verifycode

			$sysusers =& FLEA::getSingleton('Model_SysUsers');//instantiate user class
			$sysusers -> encodeMethod = PWD_MD5;//set encode
			$user = $sysusers->findByUsername($username);//check user is or isn't existence

			/*
			 * check user
			 */
			if (!$user) {
				$msg = _T('ui_l_invalid_username');//point user isn't existence
				js_alert($msg,'',url('Default'));
			}

			/*
			 * check password
			 */
			if (!$sysusers->checkPassword($password,$user[$sysusers->passwordField])){
				$msg = _T('ui_l_invalid_password');//point password wrong
				js_alert($msg,'',url('Default'));
			}

			/*
			 * save user information
			 */
			$data = array();
			if(isset($user[$sysusers->usernameField])&&!empty($user[$sysusers->usernameField])){
				$data['USERNAME'] = $user[$sysusers->usernameField];
				$data['ID'] = $user[$sysusers->primaryKey];
			}else{
				$data['USERNAME'] = $email[$sysusers->usernameField];
				$data['ID'] = $email[$sysusers->primaryKey];
			}

			$sysusers -> functionFields = FLEA::getAppInf('functionFields');//include user table fields
			$sysusers -> validateUser($username,$password);//update user login data

			$rbac =& FLEA::getSingleton('FLEA_Rbac');
			$rbac->setUser($data, $sysusers->fetchRoles($user));

			redirect(url('User','Index'));

		}while(false);

		redirect(url('Default'));
	}

	function actionInfo(){
		$this ->_ModelUserInfo = & FLEA::getSingleton('Model_UserInfo');
		$userInfo = $this ->_ModelUserInfo->analysisUserInfo($this ->_ModelUserInfo,$this->user['ID']);//get more user's information

		$viewData = array(
			'siteInfo' => $this -> siteInfo,
			'user' => $this->user,
			'userInfo' => $userInfo,
		);
		$this->_executeView('UserInfo.html',$viewData);
	}

	function actionLogout(){
		session_destroy();
		redirect(url('Default'));
	}

	function actionRegister(){
		$viewData = array(
			'siteInfo' => $this -> siteInfo,
		);

		$this->_executeView('UserRegister.html',$viewData);
	}
	function actionRegisterNext(){
		foreach($_POST as $key => $value){
			$$key = addslashes(trim($value));
		}

		if($password != $password2){
			$msg = '两次输入的密码不同';
			js_alert($msg,'',url('User','Register'));
		}else{
			$list = array(
				'username' => $username,
				'password' => $password,
				'email' => $email,
				'roles' => array(
					'0' => array(
						'role_id' => '2',
			),
			),
				'declareperson' => array(
					'name' => $name,
					'cellphone' => $cellphone,
			),
			);
		}

		$sysusers =& FLEA::getSingleton('Model_SysUsers');
		$sysusers -> encodeMethod = PWD_MD5;//set encode
		$sysusers -> functionFields = FLEA::getAppInf('functionFields');

		do{
			$user = $sysusers->findByUsername($name);
			if ($user) {
				$msg = '用户名已经存在，请更换用户名';
				js_alert($msg,'',url('User','Register'));
			}
			$sysusers -> create($list);
			$msg = '用户注册成功，返回首页登录';
			js_alert($msg,'',url('Default'));
		}
		while(false);

		redirect(url('User','Register'));
	}
	function actionChangeInfo(){
		//		dump($_POST);exit;
		foreach($_POST as $key => $value){
			$$key = addslashes(trim($value));
		}

		/*$list = array(
		 'user_id' => $user_id,
		 'username' => $username,
		 'email' => $email,
		 'declareperson' => array(
		 'id'=>$declareperson_id,
		 'name' => $name,
		 'cellphone' => $cellphone,
		 ),
		 );*/

		$sysusers =& FLEA::getSingleton('Model_SysUsers');
		$sysusers -> encodeMethod = PWD_MD5;//set encode
		$sysusers -> functionFields = FLEA::getAppInf('functionFields');//set user's table fields


		if($password){

			if($password != $password2){
				$msg = '两次输入的密码不同';
				js_alert($msg,'',url('User','Info'));
			}else{
				if(!$sysusers ->changePassword($username, $oldpassword, $password)){
					$msg = '原始密码错误';
					js_alert($msg,'',url('User','Info'));
				}else{
					$msg = '密码已经修改，请使用新密码登录';
					js_alert($msg,'',url('User','Logout'));
				}
			}
		}else{
			$msg = '请输入密码';
			js_alert($msg,'',url('User','Info'));
		}
	}
}
?>